<template>
    <div class="w-full ">
        <!-- 搜索框 -->
        <div class="flex gap-4 mb-4">
            <el-button type="danger" size="large" @click="batchDelete">批量取消关注</el-button>
        </div>
        <!-- 数据列表 -->
        <el-table ref="multipleTableRef" @selection-change="handleSelectionChange" :data="ShopList"
            header-row-class-name="orange-header" border style="margin: 20px 0;">
            <el-table-column type="selection" width="55" />
            <el-table-column label="店招/店招简称">
                <template #default="scope">
                    <p>店招简称：{{ scope.row.abbreviation }}</p>
                    <p>店招：{{ scope.row.shopSing }}</p>
                </template>
            </el-table-column>
            <el-table-column prop="attribute" label="厂家属性" />
            <el-table-column label="产品链接" width="350" >
                <template #default="scope">
                    <el-button type="primary" @click="handleNewProduct(scope.row)">最新上款</el-button>
                    <el-button type="warning"@click="handleStockProduct(scope.row)" >库存产品</el-button>
                    <el-button type="info"  @click="handleRemoveProduct(scope.row)">下架产品</el-button>
                </template>
            </el-table-column>
            <el-table-column label="联系方式">
                <template #default="scope">
                    <div class="mb-2">
                        <i class="iconfont icon-QQ">{{ scope.row.qq }}</i>
                        <img src="@/assets/img/qq_chat.png">
                    </div>
                    <div class="mb-2">
                        <i class="iconfont icon-phone">
                            <template v-for="phone in scope.row.phones">{{ phone }}&nbsp;<br></template>
                        </i>
                    </div>
                    <div class="mb-2">
                        <i class="iconfont icon-dizhi">
                            <template v-for="(address, index) in scope.row.addresses">{{ address }}&nbsp;<br></template>
                        </i>
                    </div>


                </template>
            </el-table-column>
            <el-table-column prop="followTime" label="关注时间"></el-table-column>
            <el-table-column label="操作">
                <template #default="scope">
                    <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">取消关注</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-8 flex justify-center">
            <el-pagination v-model:current-page="currentPage" :page-size="20" :total="pageTotal" prev-text="上一页"
                next-text="下一页" layout="prev, pager, next,total,jumper" @current-change="handleCurrentChange" />
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted, } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
//加载json模拟数据
import loadLocalJson from "@/https/local/localJson.js";
const router = useRouter()
//获取下载的商品信息
const ShopList = ref()
const getShopList = async () => {
    await loadLocalJson("json/downloadManage/manufacturer.json").then((res) => {
        ShopList.value = res.data.list;
        pageTotal.value = res.data.total;
    });
}
//分页相关的数据
const currentPage = ref(1);
const pageTotal = ref(0);
const handleCurrentChange = (val) => {
    console.log(`current page: ${val}`);
    //todo 做分页查询
};

const multipleTableRef = ref()
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
//todo删除
const handleDelete = (index, row) => {
    console.log(index, row)
    ElMessageBox.confirm(
        '是否取消关注?',
        '信息',
        {
            cancelButtonText: '确定',
            confirmButtonText: '删除',
            type: 'warning',
        }
    )
        .then(() => {
            //todo 调用后端接口删除
            ElMessage({
                type: 'success',
                message: '取消关注成功',
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '未取消关注',
            })
        })
}
//批量删除
const batchDelete = () => {
    //选择要删除的商品
    console.log(multipleSelection.value.map(item => item.id));
    if (multipleSelection.value.length === 0) {
        return ElMessage({
            type: 'warning',
            message: '请选择要取消关注的商品',
        })
    }
    //todo 批量删除调用后端接口删除数据
    ElMessageBox.confirm('确定要取消关注这些厂家吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        //todo 删除成功
        ElMessage({
            type: 'success',
            message: '取消关注成功',
        })
        //刷新数据
        getShopList()
    }).catch(() => { })
}
//点击最新上款
const  handleNewProduct = (row) => {
    router.push({
        path: '/shopHome',
        query: {
            id: row.id,
            tabType: 'new'
        } 
    })
}
//点击库存产品
const  handleStockProduct = (row) => {
    router.push({
        path: '/shopHome',
        query: {
            id: row.id,
            tabType: 'stock'
        } 
    })
}
//点击下架产品
const  handleRemoveProduct = (row) => {
    router.push({
        path: '/shopHome',
        query: {
            id: row.id,
            tabType: 'remove'
        } 
    })
}
onMounted(async () => {
    await getShopList()
    
})
</script>
<style lang="less" scoped></style>
<style>
/* 表格整体背景色 */
.el-table {
    /* background-color: #666 !important; */
    /* 浅黄色背景 */
}

/* 表头样式 */
.orange-header th {
    background-color: #eee !important;
    /* 橘色背景 */
    color: #777 !important;
    /*文字颜色*/
}

/* 可选：hover效果 */
.el-table--enable-row-hover .el-table__body tr:hover>td {
    background-color: #ffefd5 !important;
}
</style>